package com.jinkaimen.goodcash.ui.weiget.photopicker;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;


import com.jinkaimen.goodcash.R;
import com.jinkaimen.goodcash.ui.weiget.dialog.CustomButtons;
import com.jinkaimen.goodcash.ui.weiget.photopicker.widget.ViewPagerFixed;

import java.util.ArrayList;


/**
 * Created by foamtrace on 2015/8/25.
 */
public class PhotoPreviewActivity extends AppCompatActivity implements PhotoPagerAdapter.PhotoViewClickListener {

    public static final String EXTRA_PHOTOS = "extra_photos";
    public static final String EXTRA_ALL_PHOTOS = "extra_all_photos";
    public static final String EXTRA_CURRENT_ITEM = "extra_current_item";

    /**
     * 选择结果，返回为 ArrayList&lt;String&gt; 图片路径集合
     */
    public static final String EXTRA_RESULT = "preview_result";

    /**
     * 预览请求状态码
     */
    public static final int REQUEST_PREVIEW = 99;

    private ArrayList<String> selectPaths = new ArrayList<>();
    private ArrayList<String> allPaths = new ArrayList<>();
    private ViewPagerFixed mViewPager;
    private PhotoPagerAdapter mPagerAdapter;
    private int currentItem = 0;


    private TextView tv_choose;
    private boolean isClickImage;
    private TextView tv_title_bar_title;
    private TextView discard;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_image_preview);
        isClickImage = getIntent().getBooleanExtra(PhotoPickerActivity.EXTRA_CLICK_IMAGE, false);
        currentItem = getIntent().getIntExtra(EXTRA_CURRENT_ITEM, 0);

        ArrayList<String> pathArr = getIntent().getStringArrayListExtra(EXTRA_PHOTOS);
        if (pathArr != null) {
            selectPaths.addAll(pathArr);
        }

        ArrayList<String> pathArr2 = getIntent().getStringArrayListExtra(PhotoPreviewActivity.EXTRA_ALL_PHOTOS);
        if (pathArr2 != null) {
            allPaths.addAll(pathArr2);
        }

        initViews();

        findViewById(R.id.rl_choose).setVisibility(isClickImage ? View.VISIBLE : View.GONE);

        tv_choose = (TextView) findViewById(R.id.tv_choose);

        tv_choose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (tv_choose.isSelected()) {
                    tv_choose.setSelected(false);
                    String path = allPaths.get(mViewPager.getCurrentItem());
                    if (selectPaths.contains(path)) {
                        selectPaths.remove(path);
                    }
                } else {
                    tv_choose.setSelected(true);
                    if (selectPaths.size() < PhotoPickerActivity.mDesireImageCount) {
                        String path = allPaths.get(mViewPager.getCurrentItem());
                        if (!selectPaths.contains(path))
                            selectPaths.add(path);
                    } else {
                        Toast.makeText(PhotoPreviewActivity.this, "已经达到最大选择数量", Toast.LENGTH_SHORT).show();
                    }
                }
                discard.setText(getString(R.string.done_with_count, selectPaths.size(), PhotoPickerActivity.mDesireImageCount));
            }
        });


        mPagerAdapter = new PhotoPagerAdapter(this, allPaths);
        mPagerAdapter.setPhotoViewClickListener(this);


        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                updateActionBarTitle();
            }

            @Override
            public void onPageSelected(int position) {
                if (discard != null)
                    discard.setText(getString(R.string.done_with_count, selectPaths.size(), PhotoPickerActivity.mDesireImageCount));
                if (selectPaths.contains(allPaths.get(position))) {
                    tv_choose.setSelected(true);
                } else {
                    tv_choose.setSelected(false);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
//        mViewPager.setAdapter(mPagerAdapter);
//        mViewPager.setOffscreenPageLimit(5);
//        mViewPager.setCurrentItem(currentItem);
        if (selectPaths.contains(allPaths.get(currentItem))) {
            tv_choose.setSelected(true);
        } else {
            tv_choose.setSelected(false);
        }
        updateActionBarTitle();
    }

    private void initViews() {
        mViewPager = (ViewPagerFixed) findViewById(R.id.vp_photos);
        LinearLayout ll_title_bar_right = (LinearLayout) findViewById(R.id.ll_title_bar_right);
        LinearLayout ll_title_bar_left = (LinearLayout) findViewById(R.id.ll_title_bar_left);
        tv_title_bar_title = (TextView) findViewById(R.id.tv_title_bar_title);
        TextView backBtn = CustomButtons.getTitleBarBackBtn(this);
        discard = CustomButtons.getTitleBarTvBtn(this,"");
//        discard.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//               if (isClickImage){
//                   onBackPressed();
//                   return;
//               }
//                final int index = mViewPager.getCurrentItem();
//                final String deletedPath = selectPaths.get(index);
//                Snackbar snackbar = Snackbar.make(getWindow().getDecorView().findViewById(android.R.id.content), R.string.deleted_a_photo,
//                        Snackbar.LENGTH_LONG);
//                if (selectPaths.size() <= 1) {
//                    // 最后一张照片弹出删除提示
//                    // show confirm dialog
//                    new AlertDialog.Builder(PhotoPreviewActivity.this)
//                            .setTitle(R.string.confirm_to_delete)
//                            .setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialogInterface, int i) {
//                                    dialogInterface.dismiss();
//                                    selectPaths.remove(index);
//                                    if (!isClickImage) {
//                                        allPaths.remove(index);
//                                    }
//                                    onBackPressed();
//                                }
//                            })
//                            .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
//                                @Override
//                                public void onClick(DialogInterface dialogInterface, int i) {
//                                    dialogInterface.dismiss();
//                                }
//                            })
//                            .show();
//                } else {
//                    snackbar.show();
//                    selectPaths.remove(index);
//                    if (!isClickImage) {
//                        allPaths.remove(index);
//                    }
//                    mPagerAdapter.notifyDataSetChanged();
//                }
//
//                snackbar.setAction(R.string.undo, new View.OnClickListener() {
//                    @Override
//                    public void onClick(View view) {
//                        if (selectPaths.size() > 0) {
//                            selectPaths.add(index, deletedPath);
//                            if (!isClickImage) {
//                                allPaths.add(index, deletedPath);
//                            }
//                        } else {
//                            selectPaths.add(deletedPath);
//                            if (!isClickImage) {
//                                allPaths.add(deletedPath);
//                            }
//                        }
//                        mPagerAdapter.notifyDataSetChanged();
//                        mViewPager.setCurrentItem(index, true);
//                    }
//                });
//            }
//        });
        backBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });
        ll_title_bar_left.addView(backBtn);
        ll_title_bar_right.addView(discard);
        if (isClickImage) {
            discard.setText(getString(R.string.done_with_count, selectPaths.size(), PhotoPickerActivity.mDesireImageCount));
        } else {
            discard.setText("删除");
        }
    }

    @Override
    public void OnPhotoTapListener(View view, float v, float v1) {
        onBackPressed();
    }

    public void updateActionBarTitle() {
        tv_title_bar_title.setText(
                getString(R.string.image_index, mViewPager.getCurrentItem() + 1, allPaths.size()));
    }

    @Override
    public void onBackPressed() {
        Intent intent = new Intent();
        intent.putExtra(EXTRA_RESULT, selectPaths);
        setResult(RESULT_OK, intent);
        finish();
        super.onBackPressed();
    }
}
